package com.liliang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liliang.entity.MaterialPage;
import com.liliang.entity.Orders;
import com.liliang.mapper.OrdersMapper;
import com.liliang.service.IOrdersService;
import com.liliang.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liliang
 * @since 2021-11-04
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    OrdersMapper om;

    @Override
    public boolean insert(Orders orders, String jhq) {
        //设置流水号
        String xqjh = RandomUtil.getliushuihao("xqjh");
        orders.setOrderNum(xqjh);
        //设置时间戳
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //替换字符串
        jhq=jhq.replace("T"," ")+":00";
        //设置计划截止时间
        orders.setEndDate(LocalDateTime.parse(jhq,df));
        //设置开始时间
        orders.setStartDate(LocalDateTime.now());
        //设置小计
        BigDecimal unitPrice =new BigDecimal(orders.getAmount());
        orders.setSumPrice(unitPrice.multiply(orders.getUnitPrice()));
        return this.save(orders);
    }

    @Override
    public MaterialPage findMaterialPage(Integer page, Integer max, Orders orders, String zt) {
        //创建分页对象
        MaterialPage materialPage=new MaterialPage();
        //得到模糊查询的数值
        String materialName = orders.getMaterialName();
        String materialCode = orders.getMaterialCode();
        if (materialName!=null){
                materialName="%"+materialName+"%";
        }
        if (materialCode!=null){
                materialCode="%"+materialCode+"%";
        }
        //模糊查询总行数
        materialPage.setMax(om.findOrdersCountLike(materialCode,materialName,zt));
        //模糊分页查询
        materialPage.setMaterial(om.findMaterialPage(page,max,materialCode,materialName,zt));
        return materialPage;
    }

    @Override
    public MaterialPage findMaterialPage2(int i, Integer rows, String sort, String order, Orders orders) {
        //创建分页对象
        MaterialPage materialPage=new MaterialPage();
        //得到模糊查询的数值
        String materialName = orders.getMaterialName();
        String materialCode = orders.getMaterialCode();
        if (materialName!=null){
            materialName="%"+materialName+"%";
        }
        if (materialCode!=null){
            materialCode="%"+materialCode+"%";
        }
        //模糊查询总行数
        materialPage.setMax(om.findOrdersCountLike2(materialCode,materialName));
        //模糊分页查询
        materialPage.setMaterial(om.findMaterialPage2(i,rows,sort,order,materialCode,materialName));
        return materialPage;
    }

    @Override
    public List<Orders> findBYstockid(long id) {
        return om.findBYstockid(id);
    }
}
